package leetcode.tree;

import leetcode.tree.common.Node;

/**
 * @author mazouri
 * @create 2021-05-04 11:01
 */
public class _116_connect {
    public Node connect(Node root) {
        if (root == null) return null;
        connetTwoNode(root.left, root.right);
        return root;
    }

    private void connetTwoNode(Node p, Node q) {
        if (p == null || q == null) return;

        p.next = q;
        connetTwoNode(p.left, p.right);
        connetTwoNode(q.left, q.right);
        connetTwoNode(p.right, q.left);
    }
}
